<template>
  <div id="airports">
    <div class="card">
      <div class="card-body">
        <form class="row">
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="名称"
              size="12"
              v-model.trim="name0"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="三字代码"
              size="8"
              v-model.trim="threeCode0"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="国家代码"
              size="8"
              v-model.trim="countryCode0"
            />
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary ms-2"
              @click.stop="reset()"
            >
              重置
            </button>

            <span class="ms-auto float-end">
              <a
                href="javascript:void(0)"
                v-on:click="newUser()"
                class="btn btn-success"
                >新建</a
              >
            </span>
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-hover table-sm">
      <thead>
        <tr>
          <th>三字代码</th>
          <th>城市代码</th>
          <th>城市名</th>
          <th>机场中文名</th>
          <th>机场英文名</th>
          <th>国家代码</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(info, index) in dataList" :key="info.id">
          <td>{{ info.threeCode }}</td>
          <td>{{ info.cityCode }}</td>
          <td>{{ info.cityName }}</td>
          <td>{{ info.airportName }}</td>
          <td>{{ info.airportEnglishName }}</td>
          <td>{{ info.countryCode }}</td>
          <td>
            <button class="btn btn-sm btn-warning" @click.stop="edit(index)">
              修改
            </button>
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-airport ref="airportModal"></my-modal-airport>
  </div>
</template>

<script>
import $ from "jquery";
import { searchAirports, createAirport } from "@/api/basic-data.js";
import MyPagination from "@/components/my-pagination.vue";
import MyModalAirport from "@/components/modal/my-modal-airport.vue";

export default {
  components: {
    MyPagination,
    MyModalAirport,
  },
  data() {
    return {
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      threeCode0: "",
      countryCode0: "",
      name0: "",

      threeCode: "",
      countryCode: "",
      cityCode: "",
      cityName: "",
      nameCn: "",
      nameEn: "",

      dataList: [],
      email: "",
      name: "",
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    if (window.matchMedia("(max-width: 576px)").matches) {
      this.$router.replace("/h5/data/airports");
    } else {
      this.search();
    }
  },
  methods: {
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      this.showLoading();
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        threeCode: this.threeCode0,
        countryCode: this.countryCode0,
        name: this.name0,
      };

      searchAirports(
        params,
        (v) => {
          this.dataList = v.dataList;
          this.sc = v.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    newUser: function () {
      this.threeCode = "";
      this.countryCode = "";
      this.cityCode = "";
      this.cityName = "";
      this.nameCn = "";
      this.nameEn = "";

      const params = {
        threeCode: this.threeCode,
        cityCode: this.cityCode,
        cityName: this.cityName,
        countryCode: this.countryCode,
        airportName: this.nameCn,
        airportEnglishName: this.nameEn,
      };

      this.$refs.airportModal
        .modal(params)
        .then((v) => {
          this.createUser(v);
        })
        .catch((ex) => {});
    },
    edit: function (index) {
      const info = this.dataList[index];
      this.threeCode = info.threeCode;
      this.countryCode = info.countryCode;
      this.cityCode = info.cityCode;
      this.cityName = info.cityName;
      this.nameCn = info.airportName;
      this.nameEn = info.airportEnglishName;

      const params = {
        threeCode: this.threeCode,
        cityCode: this.cityCode,
        cityName: this.cityName,
        countryCode: this.countryCode,
        airportName: this.nameCn,
        airportEnglishName: this.nameEn,
      };

      this.$refs.airportModal
        .modal(params)
        .then((v) => {
          this.createUser(v);
        })
        .catch((ex) => {});
    },
    createUser: function (v) {
      const params = {
        threeCode: v.threeCode,
        cityCode: v.cityCode,
        cityName: v.cityName,
        countryCode: v.countryCode,
        airportName: v.airportName,
        airportEnglishName: v.airportEnglishName,
      };

      createAirport(params, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("操作成功");
          this.search();
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
    reset: function () {
      this.threeCode0 = "";
      this.countryCode0 = "";
      this.name0 = "";
    },
  },
};
</script>
